package pcserver;


import java.awt.Font;
import java.util.Timer;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GameWindow extends JPanel implements AbstractWindow {
	private static final long serialVersionUID = 1L;

	private ImageIcon bgimage;

	private ImageBtn back;
	
	private ImageIcon backIcon;
	private ImageIcon back_on;
	private ImageIcon hintIcon;
	private ImageIcon normalIcon;
	
	
	private JLabel hint;
	private JLabel pointLabel;

	private int status;

	private GameData gameData;
	private TimeCounter timeCounter;
	private Timer timer;

	public GameWindow(GameData gameData){
		this(new ImageIcon(util.direction + "gamebg.png"), gameData);
	}

	public GameWindow(ImageIcon bgimage, GameData gameData){
		this.gameData = gameData;
		this.bgimage = bgimage;

		backIcon = new ImageIcon(util.direction + "back.png");
		back_on = new ImageIcon(util.direction + "back_1.png");
		hintIcon = new ImageIcon(util.direction + "hint.png");
		normalIcon = new ImageIcon(util.direction + "hint_0.png");
		
		back = new ImageBtn(backIcon, back_on);
		
		hint = new JLabel(normalIcon);
		status = 0;
		
		pointLabel = new JLabel("得分：0", JLabel.LEFT);
		Font font = new Font("Serief", Font.BOLD, 25);
		pointLabel.setFont(font);
		
		setOpaque(false);  
		setLayout(null);
		add(back);
		back.setBounds(590, 10, 200, 50);
		add(hint);
		hint.setBounds(42, 125, 105, 55);
		add(pointLabel);
		pointLabel.setBounds(200, 10, 200, 50);
		
	}
	
	public void start() {
		// TODO Auto-generated method stub
		timer = new Timer();
		timeCounter = new TimeCounter();
		for(int i = 0; i < gameData.count(); i++){
			timer.schedule(new BallTask(this), gameData.getTime(i));
		}
		timer.schedule(new ResultTask(this, gameData), gameData.getTime(gameData.count() - 1) + 5000);
	}
	
	public void stop(){
		if(timer != null){
			timer.cancel();
		}
	}

	@Override
	public ImageIcon getBgimage() {
		return bgimage;
	}
	
	public ImageBtn getBack() {
		return back;
	}
	
	public void hintdown() {
		
		status = 1;
		hint.setIcon(hintIcon);
		if(timeCounter != null){
			new Thread(new CountPoint(timeCounter, gameData, pointLabel)).start();
		}
	}
	
	
	public void hintup() {
		status = 0;
		hint.setIcon(normalIcon);
	}

	public int getStatus() {
		return status;
	}


}
